package web;

import Utils.data.DataConverters;
import Utils.io.json.JacksonUtils;
import Utils.web.WebUtils;
import Utils.web.servlet.BaseServlet;
import Utils.web.servlet.ServletUtils;
import pojo.Task;
import pojo.User;
import service.Impl.UserServiceImpl;
import service.UserService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;

@WebServlet(name = "UserServlet", value = "/UserServlet")
public class UserServlet extends BaseServlet {
    private UserService userService = new UserServiceImpl();

    protected void register(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, SQLException {
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String kaptcha = request.getParameter("kaptcha");

        String token = WebUtils.getKaptchaToken(request);
        if (token != null && token.equalsIgnoreCase(kaptcha)) {
            User user = new User(null, username, password, null);
            if (userService.exists(user)) {
                request.setAttribute("msg", "用户名已经存在！");
                request.getRequestDispatcher("/pages/user/register.jsp").forward(request, response);
            } else {
                User registerUser = userService.register(user);
                request.getSession().setAttribute("user", registerUser);
                response.sendRedirect(request.getContextPath() + "/pages/user/register_success.jsp");
            }
        } else {
            request.setAttribute("msg", "验证码错误！");
            request.setAttribute("username", username);
            request.setAttribute("password", password);
            request.getRequestDispatcher("/pages/user/register.jsp").forward(request, response);
        }
    }

    protected void login(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, SQLException {
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String kaptcha = request.getParameter("kaptcha");

        User user = new User(null, username, password, null);
        String token = WebUtils.getKaptchaToken(request);
        if (token != null && token.equalsIgnoreCase(kaptcha)) {
            User loginUser = userService.login(user);
            if (loginUser == null) {
                request.setAttribute("msg", "用户或密码错误！");
                request.setAttribute("username", username);
                request.getRequestDispatcher("/pages/user/login.jsp").forward(request, response);
            } else {
                request.getSession().setAttribute("user", loginUser);
                response.sendRedirect(request.getContextPath() + "/pages/user/login_success.jsp");
            }
        } else {
            request.setAttribute("msg", "验证码错误！");
            request.setAttribute("username", username);
            request.setAttribute("password", password);
            request.getRequestDispatcher("/pages/user/login.jsp").forward(request, response);
        }
    }

    protected void logout(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        WebUtils.terminateSession(request);
        response.sendRedirect(request.getContextPath() + "/pages/user/login.jsp");
    }

    protected void exitsAjaxUsername(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, SQLException {
        String username = request.getParameter("username");
        Map<String, Object> resultMap=new HashMap<>();
        resultMap.put("exitsAjaxUsername", userService.exists(username));
        response.getWriter().write(JacksonUtils.getInstance().serialize(resultMap));
    }
}
